/**
 * Created by lwy on 2017/9/4.
 */

var userId = GetQueryString('userId');
$(function () {
    $.ajax({
        url:'/user/userMSG.json',
        dataType:'json',
        data:{
            id:userId,
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                $('#code1').val('邀请人：');
                $('#userName').html(data.datas.realName);
            }
        }
    })
})

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){
    if(!$('#phone').val().match(/^1[2-9][0-9]{9}$/)){
        mui.toast('请输入正确手机号码');
        return;
    }

    if(!ifCyber()){
        mui.toast('请检查网络');
    }

    $.ajax({
        url:'/user/checkIfNewUser.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                noRegister();
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==-1){
                mui.alert('您已注册过账号，请登录',function(){
                    window.location.href="login.html"
                })
            }
        }
    })

    //验证该手机号是否发送过验证码
    function noRegister() {
        $.ajax({
            url:'/imageVerifyC/ifCode.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
            },
            type:'post',
            success:function (data) {
                if(data.ifCode){
                    //如果为true，从本地获取图片验证码
                    verifyCode = getItem('verifyCode');
                    verify();
                }else{
                    //如果为false，需要输入
                    var timestamp = (new Date()).valueOf();
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                    $('#code_input').val('');
                    $('.zhezhao').show();
                }
            }
        })
    }

}

document.getElementById('code_input').oninput=function () {
    if($(this).val().length==5){
        $.ajax({
            //验证码是否正确接口
            url:'/imageVerifyC/verifyCode.json',
            dataType:'json',
            data:{
                mobile:$('#phone').val(),
                validateCode:$('#code_input').val(),
            },
            type:'post',
            success:function (data) {
                if(data.code!=1){
                    mui.toast(data.msg);
                    return;
                }
                if(data.code==1){
                    if(data.verifyCode){
                        $('.zhezhao').hide();
                        twice=true;
                        verifyCode = $('#code_input').val();
                        setItem('verifyCode',$('#code_input').val());
                        verify();
                    }else{
                        mui.toast('验证码错误');
                        var timestamp = (new Date()).valueOf();
                        $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val()+"&t="+timestamp);
                        return;
                    }
                }
            }
        })
    }
}

//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

function verify() {
    $(".code-button").html("发送中…");
    $(".code-button").unbind();
    if(!ifCyber()){
        mui.toast('请检查网络');
    }
    $.ajax({
        url:'/user/sendMobileCode_v2.json',
        data:{
            mobile:$('#phone').val(),
            platform:'wap',
            validateCode:verifyCode,
        },
        dataType:'json',
        success:function(data) {
            if(data.code==0){
                if(data.msg=='图形验证码验证失败'){
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+$("#phone").val());
                    $('#code_input').val('');
                    $('.zhezhao').show();
                    return;
                }
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $(".code-button").bind('click',sendCode);
                return;
            }
            if(data.code==-1){
                mui.alert('您已注册过账号，请登录',function(){
                    window.location.href="login.html"
                })
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};

//注册
$('.register-btn').click(function(){

    if($('#phone').val()==""){
        mui.toast('请输入手机号');
        return;
    }
    if($('#code2').val()==""){
        mui.toast('请输入验证码');
        return;
    }
    if($('#code2').val().length!=6){
        mui.toast('验证码格式不正确');
        return;
    }

    $.ajax({
        url:'/user/checkMobileCode_v2.json',
        dataType:'json',
        data:{
            mobile:$('#phone').val(),
            mobile_code:$('#code2').val(),
            platform:'wap',
            id:userId,
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                //var btnArray = ['取消', '确定'];
                //mui.confirm('推荐人手机号：'+data.datas.upperPhone+'','推荐人姓名：'+data.datas.upperName+'',  btnArray, function(e) {
                    //if (e.index == 1) {
                        $.ajax({
                            url:'/user/register.json',
                            dataType:'json',
                            data:{
                                mobile:$('#phone').val(),
                                id:userId,
                                isdzp:GetQueryString('isdzp'), //判断是否大转盘
                            },
                            type:'post',
                            success:function(data){
                                if(data.code==1){
                                    setItem('token',data.datas.token);
                                    setItem('phone',$('#phone').val());
                                    window.location.href='index.html';
                                }
                            }
                        })
                    //} else {
                    //}
                //})
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
            if(data.code==-1){
                mui.alert('您已注册过账号，请登录',function(){
                    window.location.href="login.html"
                })
            }
        }
    })
})